enum and flags type creation can now be done through
authorTim Janik <timj@gtk.org>
Wed, 10 Jun 1998 04:55:22 +0000 (04:55 +0000)
committerTim Janik <timj@src.gnome.org>
Wed, 10 Jun 1998 04:55:22 +0000 (04:55 +0000)
Wed Jun 10 06:25:17 1998  Tim Janik  <timj@gtk.org>

        * gtk/gtktypeutils.h:
        * gtk/gtktypeutils.c: enum and flags type creation can now be done
        through gtk_type_register_enum() and gtk_type_register_flags(),
        which allow to specify the enum value arrays directly.
        the NULL terminated value arrays can be retrived through
        gtk_type_enum_get_values() and gtk_type_flags_get_values();

ChangeLog
ChangeLog.pre-2-0
ChangeLog.pre-2-10
ChangeLog.pre-2-2
ChangeLog.pre-2-4
ChangeLog.pre-2-6
ChangeLog.pre-2-8
gtk/gtktypeutils.c
gtk/gtktypeutils.h

index 9476b243e777a0c1cb127f4307ebb8d3ff9c4033..ccfee9494077306429cc11dd543b62b75d3b0fbe 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+Wed Jun 10 06:25:17 1998  Tim Janik  <timj@gtk.org>
+
+       * gtk/gtktypeutils.h: 
+       * gtk/gtktypeutils.c: enum and flags type creation can now be done
+       through gtk_type_register_enum() and gtk_type_register_flags(),
+       which allow to specify the enum value arrays directly.
+       the NULL terminated value arrays can be retrived through
+       gtk_type_enum_get_values() and gtk_type_flags_get_values();
+
 1998-06-09  Raja R Harinath  <harinath@cs.umn.edu>
 
        * gtk/Makefile.am (gtkmarshal.c gtkmarshal.h): Pass $srcdir in
index 9476b243e777a0c1cb127f4307ebb8d3ff9c4033..ccfee9494077306429cc11dd543b62b75d3b0fbe 100644 (file)
@@ -1,3 +1,12 @@
+Wed Jun 10 06:25:17 1998  Tim Janik  <timj@gtk.org>
+
+       * gtk/gtktypeutils.h: 
+       * gtk/gtktypeutils.c: enum and flags type creation can now be done
+       through gtk_type_register_enum() and gtk_type_register_flags(),
+       which allow to specify the enum value arrays directly.
+       the NULL terminated value arrays can be retrived through
+       gtk_type_enum_get_values() and gtk_type_flags_get_values();
+
 1998-06-09  Raja R Harinath  <harinath@cs.umn.edu>
 
        * gtk/Makefile.am (gtkmarshal.c gtkmarshal.h): Pass $srcdir in
index 9476b243e777a0c1cb127f4307ebb8d3ff9c4033..ccfee9494077306429cc11dd543b62b75d3b0fbe 100644 (file)
@@ -1,3 +1,12 @@
+Wed Jun 10 06:25:17 1998  Tim Janik  <timj@gtk.org>
+
+       * gtk/gtktypeutils.h: 
+       * gtk/gtktypeutils.c: enum and flags type creation can now be done
+       through gtk_type_register_enum() and gtk_type_register_flags(),
+       which allow to specify the enum value arrays directly.
+       the NULL terminated value arrays can be retrived through
+       gtk_type_enum_get_values() and gtk_type_flags_get_values();
+
 1998-06-09  Raja R Harinath  <harinath@cs.umn.edu>
 
        * gtk/Makefile.am (gtkmarshal.c gtkmarshal.h): Pass $srcdir in
index 9476b243e777a0c1cb127f4307ebb8d3ff9c4033..ccfee9494077306429cc11dd543b62b75d3b0fbe 100644 (file)
@@ -1,3 +1,12 @@
+Wed Jun 10 06:25:17 1998  Tim Janik  <timj@gtk.org>
+
+       * gtk/gtktypeutils.h: 
+       * gtk/gtktypeutils.c: enum and flags type creation can now be done
+       through gtk_type_register_enum() and gtk_type_register_flags(),
+       which allow to specify the enum value arrays directly.
+       the NULL terminated value arrays can be retrived through
+       gtk_type_enum_get_values() and gtk_type_flags_get_values();
+
 1998-06-09  Raja R Harinath  <harinath@cs.umn.edu>
 
        * gtk/Makefile.am (gtkmarshal.c gtkmarshal.h): Pass $srcdir in
index 9476b243e777a0c1cb127f4307ebb8d3ff9c4033..ccfee9494077306429cc11dd543b62b75d3b0fbe 100644 (file)
@@ -1,3 +1,12 @@
+Wed Jun 10 06:25:17 1998  Tim Janik  <timj@gtk.org>
+
+       * gtk/gtktypeutils.h: 
+       * gtk/gtktypeutils.c: enum and flags type creation can now be done
+       through gtk_type_register_enum() and gtk_type_register_flags(),
+       which allow to specify the enum value arrays directly.
+       the NULL terminated value arrays can be retrived through
+       gtk_type_enum_get_values() and gtk_type_flags_get_values();
+
 1998-06-09  Raja R Harinath  <harinath@cs.umn.edu>
 
        * gtk/Makefile.am (gtkmarshal.c gtkmarshal.h): Pass $srcdir in
index 9476b243e777a0c1cb127f4307ebb8d3ff9c4033..ccfee9494077306429cc11dd543b62b75d3b0fbe 100644 (file)
@@ -1,3 +1,12 @@
+Wed Jun 10 06:25:17 1998  Tim Janik  <timj@gtk.org>
+
+       * gtk/gtktypeutils.h: 
+       * gtk/gtktypeutils.c: enum and flags type creation can now be done
+       through gtk_type_register_enum() and gtk_type_register_flags(),
+       which allow to specify the enum value arrays directly.
+       the NULL terminated value arrays can be retrived through
+       gtk_type_enum_get_values() and gtk_type_flags_get_values();
+
 1998-06-09  Raja R Harinath  <harinath@cs.umn.edu>
 
        * gtk/Makefile.am (gtkmarshal.c gtkmarshal.h): Pass $srcdir in
index 9476b243e777a0c1cb127f4307ebb8d3ff9c4033..ccfee9494077306429cc11dd543b62b75d3b0fbe 100644 (file)
@@ -1,3 +1,12 @@
+Wed Jun 10 06:25:17 1998  Tim Janik  <timj@gtk.org>
+
+       * gtk/gtktypeutils.h: 
+       * gtk/gtktypeutils.c: enum and flags type creation can now be done
+       through gtk_type_register_enum() and gtk_type_register_flags(),
+       which allow to specify the enum value arrays directly.
+       the NULL terminated value arrays can be retrived through
+       gtk_type_enum_get_values() and gtk_type_flags_get_values();
+
 1998-06-09  Raja R Harinath  <harinath@cs.umn.edu>
 
        * gtk/Makefile.am (gtkmarshal.c gtkmarshal.h): Pass $srcdir in
index 607e0ba3edb84657b5a8cee941a9f767a3718c94..578744ce2441158207c9eef1f66aaa5a8a4e7281 100644 (file)
@@ -61,8 +61,6 @@ static void  gtk_type_init_builtin_types      (void);
 static GtkTypeNode *type_nodes = NULL;
 static guint        n_type_nodes = 0;
 static GHashTable  *type_name_2_type_ht = NULL;
-static const gchar *key_enum_vals = "gtk-type-enum-values";
-static guint        key_id_enum_vals = 0;
 
 
 static GtkTypeNode*
@@ -518,46 +516,6 @@ gtk_arg_copy (GtkArg         *src_arg,
   return dest_arg;
 }
 
-GtkEnumValue*
-gtk_type_enum_get_values (GtkType      enum_type)
-{
-  if (gtk_type_is_a (enum_type, GTK_TYPE_ENUM) ||
-      gtk_type_is_a (enum_type, GTK_TYPE_FLAGS))
-    return g_dataset_id_get_data (gtk_type_name (enum_type), key_id_enum_vals);
-
-  g_warning ("gtk_type_enum_get_values(): type `%s' is not derived from `enum' or `flags'",
-            gtk_type_name (enum_type));
-
-  return NULL;
-}
-
-void
-gtk_type_enum_set_values (GtkType       enum_type,
-                         GtkEnumValue *values)
-{
-  if (!key_id_enum_vals)
-    key_id_enum_vals = g_dataset_force_id (key_enum_vals);
-    
-  if (gtk_type_is_a (enum_type, GTK_TYPE_ENUM) ||
-      gtk_type_is_a (enum_type, GTK_TYPE_FLAGS))
-    {
-      gchar *type_name;
-
-      type_name = gtk_type_name (enum_type);
-      if (g_dataset_id_get_data (type_name, key_id_enum_vals))
-       g_warning ("gtk_type_enum_set_values(): enum values for `%s' are already set",
-                  type_name);
-      else
-       g_dataset_id_set_data (type_name,
-                              key_id_enum_vals,
-                              values);
-      return;
-    }
-
-  g_warning ("gtk_type_enum_set_values(): type `%s' is not derived from `enum' or `flags'",
-            gtk_type_name (enum_type));
-}
-
 static void
 gtk_type_class_init (GtkTypeNode *node)
 {
@@ -592,45 +550,139 @@ gtk_type_class_init (GtkTypeNode *node)
          object_class->nsignals = 0;
          object_class->n_args = 0;
        }
-      
+
+      /* class_init_func is used as data pointer for
+       * class_size==0 types
+       */
       if (node->type_info.class_init_func)
        (* node->type_info.class_init_func) (node->klass);
     }
 }
 
-static guint
-gtk_type_name_hash (const char *key)
+GtkEnumValue*
+gtk_type_enum_get_values (GtkType      enum_type)
 {
-  guint result;
+  if (GTK_FUNDAMENTAL_TYPE (enum_type) == GTK_TYPE_ENUM ||
+      GTK_FUNDAMENTAL_TYPE (enum_type) == GTK_TYPE_FLAGS)
+    {
+      GtkTypeNode *node;
 
-  result = 0;
-  while (*key)
-    result += (result << 3) + *key++;
+      LOOKUP_TYPE_NODE (node, enum_type);
+      if (node)
+       return (GtkEnumValue*) node->type_info.class_init_func;
+    }
 
-  return result;
+  g_warning ("gtk_type_enum_get_values(): type `%s' is not derived from `enum' or `flags'",
+            gtk_type_name (enum_type));
+
+  return NULL;
 }
 
-static gint
-gtk_type_name_compare (const char *a,
-                      const char *b)
+GtkFlagValue*
+gtk_type_flags_get_values (GtkType        flags_type)
 {
-  return (strcmp (a, b) == 0);
+  return gtk_type_enum_get_values (flags_type);
 }
 
 static inline GtkType
-gtk_type_register_builtin (gchar   *name,
-                          GtkType parent)
+gtk_type_register_intern (gchar        *name,
+                         GtkType       parent,
+                         GtkEnumValue *values)
 {
+  GtkType type_id;
   GtkTypeInfo info;
 
   info.type_name = name;
-  info.object_size = info.class_size = 0;
-  info.class_init_func = NULL;
+  info.object_size = 0;
+  info.class_size = 0;
+  info.class_init_func = (void*) values;
   info.object_init_func = NULL;
   info.arg_set_func = NULL;
   info.arg_get_func = NULL;
 
-  return gtk_type_create (parent, name, &info);
+  /* relookup pointers afterwards.
+   */
+  type_id = gtk_type_create (parent, name, &info);
+
+  if (type_id && values)
+    {
+      guint i;
+
+      /* check for proper type consistency and NULL termination
+       * of value array
+       */
+      g_assert (GTK_FUNDAMENTAL_TYPE (type_id) == GTK_TYPE_ENUM ||
+               GTK_FUNDAMENTAL_TYPE (type_id) == GTK_TYPE_FLAGS);
+
+      i = 0;
+      while (values[i].value_name)
+       i++;
+
+      g_assert (values[i].value_name == NULL && values[i].value_nick == NULL);
+    }
+
+  return type_id;
+}
+
+GtkType
+gtk_type_register_enum (const gchar    *type_name,
+                       GtkEnumValue   *values)
+{
+  GtkType type_id;
+  gchar *name;
+
+  g_return_val_if_fail (type_name != NULL, 0);
+
+  name = g_strdup (type_name);
+
+  /* relookup pointers afterwards.
+   */
+  type_id = gtk_type_register_intern (name, GTK_TYPE_ENUM, values);
+
+  if (!type_id)
+    g_free (name);
+
+  return type_id;
+}
+
+GtkType
+gtk_type_register_flags (const gchar    *type_name,
+                        GtkFlagValue   *values)
+{
+  GtkType type_id;
+  gchar *name;
+
+  g_return_val_if_fail (type_name != NULL, 0);
+
+  name = g_strdup (type_name);
+
+  /* relookup pointers afterwards.
+   */
+  type_id = gtk_type_register_intern (name, GTK_TYPE_FLAGS, values);
+
+  if (!type_id)
+    g_free (name);
+
+  return type_id;
+}
+
+static guint
+gtk_type_name_hash (const char *key)
+{
+  guint result;
+
+  result = 0;
+  while (*key)
+    result += (result << 3) + *key++;
+
+  return result;
+}
+
+static gint
+gtk_type_name_compare (const char *a,
+                      const char *b)
+{
+  return (strcmp (a, b) == 0);
 }
 
 extern void gtk_object_init_type (void);
@@ -690,8 +742,9 @@ gtk_type_init_builtin_types (void)
     {
       GtkType type_id;
 
-      type_id = gtk_type_register_builtin (fundamental_info[i].name,
-                                     GTK_TYPE_INVALID);
+      /* relookup pointers afterwards.
+       */
+      type_id = gtk_type_register_intern (fundamental_info[i].name, GTK_TYPE_INVALID, NULL);
 
       g_assert (type_id == fundamental_info[i].type_id);
     }
@@ -704,20 +757,14 @@ gtk_type_init_builtin_types (void)
 
       g_assert (builtin_info[i].type_name != NULL);
 
-      type_id =
-       gtk_type_register_builtin (builtin_info[i].type_name,
-                                  builtin_info[i].parent);
+      /* relookup pointers afterwards.
+       */
+      type_id = gtk_type_register_intern (builtin_info[i].type_name,
+                                         builtin_info[i].parent,
+                                         builtin_info[i].values);
 
       g_assert (type_id != GTK_TYPE_INVALID);
 
       (*builtin_info[i].type_id) = type_id;
-
-      if (builtin_info[i].values)
-       {
-         g_assert (gtk_type_is_a (type_id, GTK_TYPE_ENUM) ||
-                   gtk_type_is_a (type_id, GTK_TYPE_FLAGS));
-
-         gtk_type_enum_set_values (type_id, builtin_info[i].values);
-       }
     }
 }
index b7885427607f73249c9cce7da53dfa926541a836..720fc9ffa7fd397bafe3cd0a688716fc369d71e3 100644 (file)
@@ -187,37 +187,40 @@ struct _GtkEnumValue
 };
 
 
-void    gtk_type_init              (void);
-GtkType         gtk_type_unique            (guint        parent_type,
-                                    GtkTypeInfo *type_info);
-void    gtk_type_set_chunk_alloc   (GtkType      type,
-                                    guint        n_chunks);
-gchar*  gtk_type_name              (guint        type);
-GtkType         gtk_type_from_name         (const gchar *name);
-GtkType         gtk_type_parent            (GtkType      type);
-gpointer gtk_type_class                    (GtkType      type);
-gpointer gtk_type_parent_class     (GtkType      type);
-gpointer gtk_type_new              (GtkType      type);
-void    gtk_type_free              (GtkType      type,
-                                    gpointer     mem);
-void    gtk_type_describe_heritage (GtkType      type);
-void    gtk_type_describe_tree     (GtkType      type,
-                                    gint         show_size);
-gint    gtk_type_is_a              (GtkType      type,
-                                    GtkType      is_a_type);
-void    gtk_type_get_arg           (GtkObject   *object,
-                                    GtkType      type,
-                                    GtkArg      *arg,
-                                    guint        arg_id);
-void    gtk_type_set_arg           (GtkObject   *object,
-                                    GtkType      type,
-                                    GtkArg      *arg,
-                                    guint        arg_id);
-GtkArg*         gtk_arg_copy               (GtkArg      *src_arg,
-                                    GtkArg      *dest_arg);
-GtkEnumValue *gtk_type_enum_get_values   (GtkType        enum_type);
-void         gtk_type_enum_set_values   (GtkType         enum_type,
-                                         GtkEnumValue   *values);
+void           gtk_type_init                   (void);
+GtkType                gtk_type_unique                 (GtkType         parent_type,
+                                                GtkTypeInfo    *type_info);
+void           gtk_type_set_chunk_alloc        (GtkType         type,
+                                                guint           n_chunks);
+gchar*         gtk_type_name                   (guint           type);
+GtkType                gtk_type_from_name              (const gchar    *name);
+GtkType                gtk_type_parent                 (GtkType         type);
+gpointer       gtk_type_class                  (GtkType         type);
+gpointer       gtk_type_parent_class           (GtkType         type);
+gpointer       gtk_type_new                    (GtkType         type);
+void           gtk_type_free                   (GtkType         type,
+                                                gpointer        mem);
+void           gtk_type_describe_heritage      (GtkType         type);
+void           gtk_type_describe_tree          (GtkType         type,
+                                                gboolean        show_size);
+gint           gtk_type_is_a                   (GtkType         type,
+                                                GtkType         is_a_type);
+void           gtk_type_get_arg                (GtkObject      *object,
+                                                GtkType         type,
+                                                GtkArg         *arg,
+                                                guint           arg_id);
+void           gtk_type_set_arg                (GtkObject      *object,
+                                                GtkType         type,
+                                                GtkArg         *arg,
+                                                guint           arg_id);
+GtkArg*                gtk_arg_copy                    (GtkArg         *src_arg,
+                                                GtkArg         *dest_arg);
+GtkType                gtk_type_register_enum          (const gchar    *type_name,
+                                                GtkEnumValue   *values);
+GtkType                gtk_type_register_flags         (const gchar    *type_name,
+                                                GtkFlagValue   *values);
+GtkEnumValue*  gtk_type_enum_get_values        (GtkType        enum_type);
+GtkFlagValue*  gtk_type_flags_get_values       (GtkType        flags_type);
 
 
 #ifdef __cplusplus